package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketRuleDao;
import com.bestcem.xm.ticket.dao.converter.CommonConverter;
import com.bestcem.xm.ticket.dao.converter.TicketRuleConverter;
import com.bestcem.xm.ticket.dao.mapper.TicketRuleDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.TicketRule;
import com.bestcem.xm.ticket.entity.mysql.TicketRuleDO;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import javax.annotation.Nonnull;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 16:42
 */
@Repository
public class TicketRuleDaoImpl implements TicketRuleDao {
    @Resource
    private TicketRuleDaoMapper ticketRuleDaoMapper;

    @Override
    public TicketRule findById(String id) {
        TicketRuleDO ticketRuleDO = ticketRuleDaoMapper.findByTicketRuleId(id);

        return TicketRuleConverter.doToDto(ticketRuleDO);
    }

    @Override
    public List<TicketRule> listByTicketConfigId(String ticketConfigId) {
        List<TicketRuleDO> ticketRuleDOList = ticketRuleDaoMapper.listByTicketConfigId(ticketConfigId);

        return TicketRuleConverter.doListToDtoList(ticketRuleDOList);
    }

    @Override
    public List<TicketRule> listByIds(List<String> ids, String ticketConfigId) {
        List<TicketRuleDO> ticketRuleDOList = ticketRuleDaoMapper
                .listByTicketConfigIdAndTicketRuleIdList(ticketConfigId, ids);

        return TicketRuleConverter.doListToDtoList(ticketRuleDOList);
    }

    @Override
    public TicketRule saveTicketRule(TicketRule ticketRule) {
        if (Objects.isNull(ticketRule)) {
            return null;
        }

        TicketRuleDO entityDo = TicketRuleConverter.dtoToDo(ticketRule);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (ObjectUtils.isEmpty(entityDo.getTicketRuleId())) {
            entityDo.setTicketRuleId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getGid())) {
            entityDo.setGid(0);
        }
        if (Objects.isNull(entityDo.getOperation())) {
            entityDo.setOperation(0);
        }
        if (Objects.isNull(entityDo.getVersion())) {
            entityDo.setVersion(0);
        }
        if (Objects.isNull(entityDo.getType())) {
            entityDo.setType(0);
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }

        ticketRuleDaoMapper.insert(entityDo);

        return TicketRuleConverter.doToDto(entityDo);
    }

    @Override
    public String updateTicketRule(String id, TicketRule ticketRule) {
        if (Objects.isNull(ticketRule)) {
            return null;
        }
        TicketRuleDO entityDo = TicketRuleConverter.dtoToDo(ticketRule);

        if (Objects.isNull(entityDo)) {
            return null;
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        entityDo.setUpdateTime(DateUtil.getCommonDate());
        ticketRuleDaoMapper.updateByTicketRuleId(entityDo);

        return id;
    }

    @Override
    public boolean updateTicketRuleCondition(@Nonnull String id, @Nonnull TicketRule.Condition condition) {
        TicketRuleDO entityDo = new TicketRuleDO();
        entityDo.setTicketRuleId(id);
        entityDo.setCondition(CommonConverter.objectToString(condition));
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }

        entityDo.setUpdateTime(DateUtil.getCommonDate());
        ticketRuleDaoMapper.updateByTicketRuleId(entityDo);

        return true;
    }
}
